package com.ogawa.project6602.dialog

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SeekBar
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.LogUtils
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.ogawa.musicmodule.*
import com.ogawa.musicmodule.bean.JustMusicVolumeEvent
import com.ogawa.musicmodule.bean.PlayMixMusicBean
import com.ogawa.project6602.R
import com.ogawa.project6602.adapter.AdjustMusicVolumeAdapter
import com.ogawa.project6602.wdiget.JustVolumeWidget
import com.ogawa.projectcommon.appdatabase.AppDatabase
import org.greenrobot.eventbus.EventBus

class AdjustMusicBottomDialog() : BottomSheetDialogFragment() {
    companion object{
        fun getInstance(): AdjustMusicBottomDialog? {
            return AdjustMusicBottomDialog()
        }
    }
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        var rootView = LayoutInflater.from(context).inflate(R.layout.dialog_adjust_music, null)
        var adjustRv = rootView.findViewById<RecyclerView>(R.id.adjustRv)
        adjustRv.setHasFixedSize(true)
        adjustRv.layoutManager = LinearLayoutManager(context)
        var adapter = AdjustMusicVolumeAdapter()
        adjustRv.adapter = adapter

        var mainJustVolumeWidget = rootView.findViewById<JustVolumeWidget>(R.id.mainJustVolumeWidget)
        mainJustVolumeWidget.setProgressListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
//                LogUtils.e("i:$p1 b:$p2")
                p0?.secondaryProgress= p1+3
                if (p2){//true拖动的
                    mainJustVolumeWidget.setProgress(p1)
                    EventBus.getDefault().post(JustMusicVolumeEvent().apply { value =
                        (p1/100.0f)
                    })
                }
            }
            override fun onStartTrackingTouch(p0: SeekBar?) {}
            override fun onStopTrackingTouch(p0: SeekBar?) {}
        })

        if (MusicService.playMainMusicBean.title.isNotEmpty()){
            mainJustVolumeWidget.setTitle(MusicService.playMainMusicBean.title)
            mainJustVolumeWidget.visibility = View.VISIBLE
            mainJustVolumeWidget.setProgress((MusicService.playMainMusicBean.volume*100).toInt())
        }else{
            mainJustVolumeWidget.visibility = View.GONE
        }



        var data = ArrayList<PlayMixMusicBean>()
        for (item in MusicService.playMixMusicBeans){
            if (item.title.isNotEmpty()
//                && item.status==MUSIC_SERVER_STATUS_SUSPEND
//                ||item.status==MUSIC_SERVER_STATUS_PLAY_ING
            ){
                data.add(item)
            }
        }
        adapter.setNewData(data)
        return rootView
    }
    override fun onStart() {
        super.onStart()
        (dialog?.getWindow())?.findViewById<View>(R.id.design_bottom_sheet)?.setBackgroundDrawable(
            ColorDrawable(
                Color.TRANSPARENT
            )
        );
    }

}